package com.ruoyi.contract.service.impl;


import java.util.List;

import com.ruoyi.contract.domain.PaymentCollection;
import com.ruoyi.contract.mapper.PaymentCollectionMapper;
import com.ruoyi.contract.service.IPaymentCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 回款Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-15
 */
@Service
public class PaymentCollectionServiceImpl implements IPaymentCollectionService
{
    @Autowired
    private PaymentCollectionMapper paymentCollectionMapper;

    /**
     * 查询回款
     *
     * @param paymentId 回款主键
     * @return 回款
     */
    @Override
    public PaymentCollection selectPaymentCollectionByPaymentId(Long paymentId)
    {
        return paymentCollectionMapper.selectPaymentCollectionByPaymentId(paymentId);
    }

    /**
     * 查询回款列表
     *
     * @param paymentCollection 回款
     * @return 回款
     */
    @Override
    public List<PaymentCollection> selectPaymentCollectionList(PaymentCollection paymentCollection)
    {
        return paymentCollectionMapper.selectPaymentCollectionList(paymentCollection);
    }

    /**
     * 新增回款
     *
     * @param paymentCollection 回款
     * @return 结果
     */
    @Override
    public long insertPaymentCollection(PaymentCollection paymentCollection)
    {
        Long i = paymentCollectionMapper.selectRemainingSum(paymentCollection.getProjectId());

        if (paymentCollection.getPaymentPrice()>i)
            return -i;
        return paymentCollectionMapper.insertPaymentCollection(paymentCollection);
    }

    /**
     * 修改回款
     *
     * @param paymentCollection 回款
     * @return 结果
     */
    @Override
    public long updatePaymentCollection(PaymentCollection paymentCollection)
    {
        Long i = paymentCollectionMapper.selectRemainingSum(paymentCollection.getProjectId());
        PaymentCollection payment = paymentCollectionMapper.selectPaymentCollectionByPaymentId(paymentCollection.getPaymentId());
        if (payment.getPaymentPrice()==null)
            payment.setPaymentPrice(0l);
        if (paymentCollection.getPaymentPrice()>i+payment.getPaymentPrice())
            return -(i+payment.getPaymentPrice());
        return paymentCollectionMapper.updatePaymentCollection(paymentCollection);
    }

    /**
     * 批量删除回款
     *
     * @param paymentIds 需要删除的回款主键
     * @return 结果
     */
    @Override
    public int deletePaymentCollectionByPaymentIds(Long[] paymentIds)
    {
        return paymentCollectionMapper.deletePaymentCollectionByPaymentIds(paymentIds);
    }

    /**
     * 删除回款信息
     *
     * @param paymentId 回款主键
     * @return 结果
     */
    @Override
    public int deletePaymentCollectionByPaymentId(Long paymentId)
    {
        return paymentCollectionMapper.deletePaymentCollectionByPaymentId(paymentId);
    }
}

